package iit.projects.etaxi.model.dao;

import iit.projects.etaxi.model.entity.Operator;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HibernateOperatorDao extends HibernateDaoSupport implements
		OperatorDao {

	final Logger log = LoggerFactory.getLogger(HibernateOperatorDao.class);
	
	@Override
	public Operator findByUserName(String username) {
		if(log.isDebugEnabled()){
			log.debug("Going to find Operator by username " + username);
		}
		
		try{
			List<?> list = getHibernateTemplate().find("from Operator o where o.username = ?", username);
			if(list != null && !list.isEmpty()){
				return (Operator)list.iterator().next();
			}
		}catch(HibernateException e){
			throw convertHibernateAccessException(e);
		}
		return null;
	}
	
	@Override
	public Operator updateOperator(Operator operator){
		if(log.isDebugEnabled()){
			log.debug("Going to update Operator " + operator.getId());
		}
		
		Session session = getSession();
		try{
			return (Operator)session.merge(operator);
		}catch(HibernateException e){
			throw convertHibernateAccessException(e);
		}
	}

}
